package cn.doctorpda.conference.ui.checked;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Calendar;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.utils.ScreenUtils;
import cn.doctorpda.conference.view.wheelview.adapter.ArrayWheelAdapter;
import cn.doctorpda.conference.view.wheelview.widget.WheelView;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 */
public class ProvinceDialogFragment extends DialogFragment implements
        OnClickListener {

    private FragmentActivity mActivity;
    private WheelView wheelPick;
    private TextView tv_ok, wheel_title;
    private OnWheelPickListener listener;
    private ArrayList<String> list = new ArrayList<String>();
    private String mtitle;

    public static ProvinceDialogFragment newInstance(ArrayList<String> list, String title) {
        ProvinceDialogFragment fragment = new ProvinceDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putStringArrayList("list", list);
        bundle.putString("title", title);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        list = getArguments().getStringArrayList("list");
        mtitle = getArguments().getString("title");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mActivity = getActivity();
        View inflate = inflater.inflate(R.layout.fragment_wheel, null);
        LinearLayout.LayoutParams la = new LinearLayout.LayoutParams(ScreenUtils.getScreenWidth(mActivity) / 2, ViewGroup.LayoutParams.WRAP_CONTENT);
        inflate.setLayoutParams(la);
        initWheel(inflate);
        getDialog().getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
//        getDialog().setCanceledOnTouchOutside(false);
        return inflate;
    }

    public void setOnDataPickListener(OnWheelPickListener listener) {
        this.listener = listener;
    }

    public interface OnWheelPickListener {
        void setOnDataPickListener(String content,int pos);
    }


    private void initWheel(View view) {

        wheelPick = (WheelView) view.findViewById(R.id.content_wheelview);
        wheel_title = (TextView) view.findViewById(R.id.wheel_title);
        wheelPick.setWheelAdapter(new ArrayWheelAdapter(getContext()));
        wheelPick.setSkin(WheelView.Skin.Holo);
        wheelPick.setWheelSize(3);//必须为奇数
        wheelPick.setWheelData(list);
        wheel_title.setText(mtitle);
        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.textColor = Color.GRAY;
        style.holoBorderColor = Color.parseColor("#DDDDDD");
        style.selectedTextColor = Color.parseColor("#448AFF");
        if (ScreenUtils.getScreenHeight(mActivity) > 1080) {
            style.textSize = 30;
            style.selectedTextSize = 36;
        } else {
            style.textSize = 20;
            style.selectedTextSize = 24;
        }
        wheelPick.setStyle(style);
//        Pattern p = Pattern.compile("[0-9]*");
//        wheelPick.setLoop(true);//放set前
        tv_ok = (TextView) view.findViewById(R.id.btn_date_ok);
        tv_ok.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        dismiss();
        switch (v.getId()) {
            case R.id.btn_date_ok:
                listener.setOnDataPickListener(wheelPick.getSelectionItem().toString(),wheelPick.getCurrentPosition());
                break;
            default:
                break;
        }
    }


}
